<template>
  <div class="main-wrap">
    <div class="operation-wrap">
      <el-button type="primary" size="mini" @click="openDia()">新增</el-button>
    </div>
    <el-table :border="true" :data="listData" :row-style="{ height: '40px' }" style="width: 100%">
      <el-table-column label="活动主题" width="180" prop="topic"></el-table-column>
      <el-table-column label="联系人" prop="associates"></el-table-column>
      <el-table-column label="联系电话" prop="phone"></el-table-column>
      <el-table-column label="活动地点" prop="location"></el-table-column>
      <el-table-column label="活动时间" prop="time"></el-table-column>
      <el-table-column label="内容" prop="details"></el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <div>
            <el-button type="text" @click="openDia(scope.row)">编辑</el-button>
            <el-button type="text" @click="del(scope.row)">删除</el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog :title="fromData.id ? '修改' : '新增'" :visible.sync="dialogVisible" width="30%">
      <el-form
        :model="fromData"
        status-icon
        ref="dataForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="图书馆" prop="libId">
          <el-select v-model="fromData.libId">
            <el-option
              v-for="item in libraryList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="活动主题" prop="topic">
          <el-input v-model="fromData.topic"></el-input>
        </el-form-item>
        <el-form-item label="联系人" prop="associates">
          <el-input v-model="fromData.associates"></el-input>
        </el-form-item>
        <el-form-item label="联系电话" prop="phone">
          <el-input v-model="fromData.phone"></el-input>
        </el-form-item>
        <el-form-item label="活动地点" prop="location">
          <el-input v-model="fromData.location"></el-input>
        </el-form-item>
        <el-form-item label="活动时间" prop="time">
          <el-date-picker
            value-format="yyyy-MM-dd HH:mm:ss"
            v-model="fromData.time"
            type="datetime"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="活动内容" prop="details">
          <el-input type="textarea" :rows="2" v-model="fromData.details"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitForm()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import request from '@/req'
export default {
  data() {
    return {
      listData: [],
      fileList: [],
      fromData: {
        id: '',
        topic: '',
        associates: '',
        phone: '',
        location: '',
        time: '',
        details: '',
        libId: '',
      },
      dialogVisible: false,
      libraryList: [],
    }
  },
  created() {
    this.getListData()
  },
  methods: {
    async getListData() {
      const res = await request.get('/activity/getActivityList')
      this.listData = res
    },
    openDia(data) {
      this.getLibrary()
      this.dialogVisible = true
      this.fromData.id = ''
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (data) {
          Object.assign(this.fromData, data)
        }
      })
    },
    async del(data) {
      const res = await request.get(`/activity/delete?id=${data.id}`)
      this.getListData()
    },
    async getLibrary() {
      const res = await request.get('/library/getLibraryList')
      this.libraryList = res
    },
    async submitForm() {
      await request.post('/activity/submit', this.fromData)
      this.dialogVisible = false
      this.getListData()
    },
  },
}
</script>

<style scoped></style>
